package com.tanhua.sso.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tanhua.sso.config.HuanXinConfig;
import com.tanhua.sso.vo.HuanXinUser;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.*;

/**
 * @description:
 * @author: Administrator
 * @date: Created in 2021/2/23 13:59
 * @version: 0.0.1
 * @modified By:
 */
@Service
public class HuanXinService {

    private static final ObjectMapper MAPPER=new ObjectMapper();

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private HuanXinConfig huanXinConfig;

    @Autowired
    private HuanXinTokenService huanXinTokenService;
    /**
     * @description 注册环信用户
     * @author
     * @date
     * @param
     *@return
     */
    public Boolean register(Long userId){
        String url=this.huanXinConfig.getUrl()+
                this.huanXinConfig.getOrgName()+
                "/"+this.huanXinConfig.getAppName()+"/users";
        //请求头信息
        HttpHeaders httpHeaders=new HttpHeaders();
        httpHeaders.add("Content-Type","application/json");
        String token=this.huanXinTokenService.getToken();
        httpHeaders.add("Authorization","Bearer "+token);

        List<HuanXinUser>huanXinUserList=new ArrayList<>();
        huanXinUserList.add(new HuanXinUser(userId.toString(), DigestUtils.md5Hex(userId+"_itcast_tanhua")));
        try {
            HttpEntity<String> httpEntity=new HttpEntity(MAPPER.writeValueAsString(huanXinUserList),httpHeaders);
            //发起请求
            ResponseEntity<String> responseEntity = this.restTemplate.postForEntity(url, httpEntity, String.class);
            return responseEntity.getStatusCodeValue()==200;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  false;
    }
    /**
     * @description 添加好友
     * @author
     * @date
     * @param
     *@return
     */
    public Boolean contactUsers(Long userId,Long friendId){
        String url=this.huanXinConfig.getUrl()+this.huanXinConfig.getOrgName()+"/"
                +this.huanXinConfig.getAppName()+"/users/"+userId+"/contacts/users/"+
                friendId;
        //请求头信息
        try {
            HttpHeaders httpHeaders=new HttpHeaders();
            httpHeaders.add("Content-Type","application/json");
            String token=this.huanXinTokenService.getToken();
            httpHeaders.add("Authorization","Bearer "+token);
            HttpEntity<String>httpEntity=new HttpEntity<>(httpHeaders);
            //发起请求
            ResponseEntity<String> responseEntity = this.restTemplate.postForEntity(url, httpEntity, String.class);
            return responseEntity.getStatusCodeValue()==200;
        } catch (Exception e) {
            e.printStackTrace();
        }
        //添加失败
        return false;
    }

    public Boolean sendMsg(String target, String msg, String type) {
        String url=this.huanXinConfig.getUrl()+this.huanXinConfig.getOrgName()+"/"
                +this.huanXinConfig.getAppName()+"/messages";
        try {
            //请求头
            HttpHeaders httpHeaders=new HttpHeaders();
            httpHeaders.add("Content-Type","application/json");
            String token=this.huanXinTokenService.getToken();
            httpHeaders.add("Authorization","Bearer "+token);
            Map<String,Object>paramMap=new HashMap<>();
            paramMap.put("target_type","users");
            paramMap.put("target", Arrays.asList(target));
            Map<String,Object>masMap=new HashMap<>();
            masMap.put("type",type);
            masMap.put("msg",msg);
            paramMap.put("msg",masMap);
            //from表示消息发送者;无此字段Server会默认设置为“from”:“admin”，有from字段但值为空串(“”)时请求失败
            HttpEntity<String> httpEntity=new HttpEntity(MAPPER.writeValueAsString(paramMap),httpHeaders);
            ResponseEntity<String> responseEntity = this.restTemplate.postForEntity(url, httpEntity, String.class);
            return responseEntity.getStatusCodeValue()==200;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
